#include <stdio.h>
#include <string.h>

#define PATH "/home/ubuntu/algorithm/API_TEST/data"

void readFile(const char* path, int* var)
{
	
	FILE *fp;
	fp = fopen(path, "r");
	if(fp == NULL)
	{
		printf("cannot open %s \n",path);
		fclose(fp);
	}
	else
	{
		fscanf(fp, "%d", var);
		fclose(fp);
		
		int len = strlen(path);
		const char* p = path;
		const char* q = p;
		while(len--)
		{
			if(*p == '/')
			{
				q = ++p;
			}	
			p++;
		}			
		printf("%s is %d \n", q, *var);
	}
}

void writeFile(const char* path, int val)
{
	FILE *fp;
	fp = fopen(path, "w");
	if(fp == NULL)
	{
		printf("cannot open %s\n",path);
		fclose(fp);
	}
	else
	{
		fprintf(fp, "%d", val);
		fclose(fp);
		
		int len = strlen(path);
		const char* p = path;
		const char* q = p;
		while(len--)
		{
			if(*p == '/')
			{
				q = ++p;
			}	
			p++;	
		}			
		printf("write %d to %s \n", val, q);
	}
}

int main()
{

	int val;
	readFile(PATH, &val);
	writeFile(PATH, 22);
	readFile(PATH, &val);	

	return 0;
}
